<extend name="Public/layout"/>
<block name="head">
<link rel="stylesheet" href="__PUBLIC__css/register.css">
<style type="text/css">
.footer{
    display: none;
}
</style>
</block>
<block name="main">
    <div class="iforgot-banner">
        <img src="__PUBLIC__images/iforgot-banner.png" alt="">
    </div>

    <div class="form">
        <div class="form-group">
            <input type="text" class="phone-number" name="tel" placeholder="请输入您的手机号码">
        </div>
        <div class="form-group">
            <input type="text" class="verify-code" name="img_code" placeholder="请输入图形验证码">
            <div class="verify-code-wrapper">
                <img src="/Home/User/img_verify" width="100%" height="100%" onclick="newgdcode(this)";>
            </div>
        </div>
        <div class="form-group">
            <input type="text" class="phone-text" name="code"  placeholder="请输入您的短信验证码">
            <div class="phone-text-wrapper">
                <button class="get-verify-code" send="1"onclick="getcode(this)">获取验证码</button>
            </div>
        </div>
        <div class="form-group split">
            <input type="text" name="password" placeholder="请设置新的登录密码">
        </div>
        <div class="form-group">
            <input type="text" name="password2" placeholder="请确认新的登录密码">
        </div>
    </div>


    <div class="btn-wrapper">
        <a href="javascript:" onclick="sub()">确认</a>
    </div>
</block>
<block name="js">
    <script type="text/javascript">
        function newgdcode(obj){
          var url = '/home/user/img_verify/time/'+ new Date().getTime()
          $(obj).attr('src',url);
        }

        var checkphone=false;
        function check_phone(){
             var phone = $('input[name="tel"]').val();
             if(!(/^1\d{10}$/.test(phone))){ 
                  msg = "请输入11位数的手机号码";
                  checkphone=false;
                  layer.open({
                    content:msg
                    ,skin: 'msg'
                    ,time: 1 //2秒后自动关闭
                  });
             }else{
                checkphone=true;
             }

              $.post("{:U('User/checkphone')}",{
                phone:phone,
              },function(res){
                if(res.err==1){
                   checkphone =true;
                }else{
                    checkphone=false;
                    layer.open({
                        content: '该手机未注册！'
                        ,skin: 'msg'
                        ,time: 1 //2秒后自动关闭
                    });
                    return;
                }
              })


        }

         //获取验证码
        function getcode(obj){
            check_phone()
            var send = $(obj).attr('send');
            console.log(obj)
            if(checkphone && send){
                event_send_sms()
            }
        }

           //验证码计时
        var timer = 60;
        function codeTimer()
        {
            timer--;
            if(timer > 0){
                $('.get-verify-code').text( timer + '秒后可重发!');
                $('.get-verify-code').attr("send",1);
                setTimeout('codeTimer()', 1000);
            }else{
                $('.get-verify-code').text('获取验证码');
                $('.get-verify-code').attr("send",0);
                timer = 60;
            }
        }

        //发送短信
        function event_send_sms(){
            var postData={
                phone: $('input[name="tel"]').val(),
                code_img:$("input[name='img_code']").val(),
                role:3
            }
            if(timer >= 60){
                var phone = $("#phoneN").val();
                    $(this).attr('disabled', 'disabled');
                    $.ajax({
                        url:'{:U("Sms/pass_send_sms_reg")}',
                        data:postData,
                        type:"post",
                        dataType:"json",
                        success:function(d){
                            if(d.err == 0){
                                codeTimer();
                            }else{
                                 layer.open({
                                    content: d.msg
                                    ,skin: 'msg'
                                    ,time: 1 //2秒后自动关闭
                                  });
                            }
                        }
                    });
                
            }
        }

        
        function sub(){
            var password =$('input[name="password"]').val();
            var password2 =$('input[name="password2"]').val();
            var phone = $('input[name="tel"]').val();
            var code = $('input[name="code"]').val();
            check_phone();
            if(password!=password2){
                layer.open({
                    content:"两次密码不一致"
                    ,skin: 'msg'
                    ,time: 1 //2秒后自动关闭
                  });
                return;
            }
            password = $.md5(password);
            if(checkphone){
                $.post("{:U('User/forgot_password')}",{
                    phone:phone,
                    code:code,
                    password:password,
                },function(res){
                    if(res.err==0){
                        layer.open({
                            content: res.msg
                            ,skin: 'msg'
                            ,time: 1
                            ,end:function(){
                                location.href="/Home/User/login"
                            }
                          });
                    }else{
                         layer.open({
                            content: res.msg
                            ,skin: 'msg'
                            ,time: 1
                          });
                    }
                })
            }


        }


    </script>
</block>2